﻿using LiveChartsCore.SkiaSharpView;
using LiveChartsCore;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using 畜牧业审核程序国产.Models.DataGridMsgs;
using 畜牧业审核程序国产.Models.SimpleFactory;
using 畜牧业审核程序国产.Models.ToolsClass;
using 畜牧业审核程序国产.Models;
using CommunityToolkit.Mvvm.Input;
using MsBox.Avalonia.Enums;
using System.IO;
using System.Drawing;
using Icon = MsBox.Avalonia.Enums.Icon;

namespace 畜牧业审核程序国产.ViewModels
{
    internal class Page5A211ViewModel : MyViewModelBase
    {
        #region 声明要用到的字段
        DataTable dtOfSingleIndicator = new DataTable();
        private readonly string withYSTableNameNow = Global.PlatformNow + Global.StrYearNow + Global.StrSeasonNow + "A211";
        private readonly string withYSTableNamePre = Global.PlatformPre + Global.StrYearPre + Global.StrSeasonPre + "A211";
        private string withYSTableNameSpring
        {
            get
            {
                if (Global.IntYearNow > 2025)
                {
                    return "统计云" + Global.StrYearNow + "第一季度A211";
                }
                else
                {
                    return "一套表" + Global.StrYearNow + "第一季度A211";
                }
            }
        }
        private string withYSTableNameSummer
        {
            get
            {
                if (Global.IntYearNow > 2025)
                {
                    return "统计云" + Global.StrYearNow + "第二季度A211";
                }
                else
                {
                    return "一套表" + Global.StrYearNow + "第二季度A211";
                }
            }
        }

        private readonly string withYSTableNameYOY = Global.PlatformYOY + Global.StrYearYOY + Global.StrSeasonYOY + "A211";

        #endregion
        #region 声明用户选择了以后,SelectedItem或SelectedValue关联的单个值.selectedItem是键值对,而selectedValue是键值对的值,配合selectedValuePath使用
        private bool _isShowSubordinateEnable = true;

        public bool IsShowSubordinateEnable
        {
            get { return _isShowSubordinateEnable; }
            set { SetProperty(ref _isShowSubordinateEnable, value); }
        }


        private KeyValuePair<string, string> _selectedCity;

        public KeyValuePair<string, string> SelectedCity
        {
            get { return _selectedCity; }
            set
            {
                SetProperty(ref _selectedCity, value);
                //选择了市以后,把县展现出来
                if (value.Value != null)
                {
                    // Models.MyDBOperator ShowOperator = new Models.MyDBOperator();改用单例模式
                    Models.MyDBOperator ShowOperator = MyDBOperator.DBOperatorInstance;
                    if (value.Value.Equals("全部"))
                    {
                        CountryCln.Clear();
                        CountryCln.Add(new KeyValuePair<string, string>("0", "全部"));
                        SelectedCountry = new KeyValuePair<string, string>("change", "change");
                        SelectedCountry = new KeyValuePair<string, string>(CountryCln[0].Key, CountryCln[0].Value);

                    }
                    else if (value.Value.Contains("未导入"))
                    {
                        return;
                    }
                    else
                    {
                        string sqlToGetAllCountry = $"select 县代码,县 from {withYSTableNameNow} where 市代码 = '{value.Key}' group by 县代码,县";
                        DataTable dtOfCountry = ShowOperator.GetDataTable(sqlToGetAllCountry);
                        if (dtOfCountry.Rows.Count == 0 || dtOfCountry == null)
                        {
                            CountryCln.Clear();
                            CountryCln.Add(new KeyValuePair<string, string>("0", "未导入本季度A211表或者导入错误"));
                            SelectedCountry = new KeyValuePair<string, string>("change", "change");
                            SelectedCountry = new KeyValuePair<string, string>(CountryCln[0].Key, CountryCln[0].Value);
                            return;

                        }
                        CountryCln.Clear();
                        CountryCln.Add(new KeyValuePair<string, string>("0", "全部"));
                        //var keys = dtOfCountry.AsEnumerable()
                        //    .Select(row => row.Field<string>("县")).ToArray();
                        for (int i = 0; i < dtOfCountry.Rows.Count; i++)
                        {
                            DataRow row = dtOfCountry.Rows[i];
                            CountryCln.Add(new KeyValuePair<string, string>(row["县代码"].ToString(), row["县"].ToString()));
                        }
                        //                        具体来说，在以下情况下，属性的 setter 不会被触发：

                        //当新值和旧值相等时：如果 SetProperty(ref _selectedTown, value); 方法内部实现了某种形式的值比较（例如使用 Equals() 或者直接比较引用），并且发现新值和旧值是相同的，那么它可能选择不执行设置操作。这通常是为了避免不必要的通知事件或者当值没有实际变化时重新计算或更新UI。
                        //在某些框架或库中，SetProperty 方法可能会检查传入的新值是否与当前存储的私有字段不同，只有在不同的情况下才会继续进行设置操作并可能触发更改通知。如果两个 KeyValuePair<string, string> 对象具有相同的键和值，它们将被视为相等，即使它们是不同的实例。
                        SelectedCountry = new KeyValuePair<string, string>("change", "change");
                        SelectedCountry = new KeyValuePair<string, string>(CountryCln[0].Key, CountryCln[0].Value);
                    }
                }
                SetProperty(ref _selectedCity, value);
            }
        }
        private KeyValuePair<string, string> _selectedCountry;

        public KeyValuePair<string, string> SelectedCountry
        {
            get { return _selectedCountry; }
            set
            {
                SetProperty(ref _selectedCountry, value);

                if (Regex.IsMatch(value.Value, "全部"))
                {
                    TownCln.Clear();
                    TownCln.Add(new KeyValuePair<string, string>("0", "全部"));
                    SelectedTown = new KeyValuePair<string, string>("change", "change");//不加这个，就无法更新，SetProperty 方法可能会检查传入的新值是否与当前存储的私有字段不同，只有在不同的情况下才会继续进行设置操作并可能触发更改通知。如果两个 KeyValuePair<string, string> 对象具有相同的键和值，它们将被视为相等，即使它们是不同的实例。
                    SelectedTown = new KeyValuePair<string, string>(TownCln[0].Key, TownCln[0].Value);
                }
                else//如果县这里选择的不是全部,而是某个具体的县,那么要把所有乡镇和"全部"一起作为townCln的值
                {
                    // Models.MyDBOperator ShowOperator = new Models.MyDBOperator();改用单例模式
                    MyDBOperator ShowOperator = MyDBOperator.DBOperatorInstance;
                    string sql = $"select 乡镇代码,乡 from {withYSTableNameNow} WHERE 县代码 = '{SelectedCountry.Key}' group by 乡镇代码,乡";
                    DataTable dt = ShowOperator.GetDataTable(sql);
                    if (dt == null || dt.Rows.Count == 0)
                    {
                        TownCln.Clear();
                        TownCln.Add(new KeyValuePair<string, string>("0", "未导入A211表或A211表导入错误"));
                        SelectedTown = new KeyValuePair<string, string>("change", "change");
                        SelectedTown = new KeyValuePair<string, string>(TownCln[0].Key, TownCln[0].Value);

                        return;
                    }
                    TownCln.Clear();
                    TownCln.Add(new KeyValuePair<string, string>("0", "全部"));
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow dtRow = dt.Rows[i];
                        TownCln.Add(new KeyValuePair<string, string>(dtRow[0].ToString(), dtRow[1].ToString()));

                    }
                    // SelectedTown = TownCln[0];
                    SelectedTown = new KeyValuePair<string, string>("change", "change");
                    SelectedTown = new KeyValuePair<string, string>(TownCln[0].Key, TownCln[0].Value);
                }
                SetProperty(ref _selectedCountry, value);
            }
        }
        private KeyValuePair<string, string> _selectedTown;

        public KeyValuePair<string, string> SelectedTown
        {
            get { return _selectedTown; }
            set
            {
                if (value.Value.Equals("全部") == false)
                {
                    IsShowSubordinateEnable = false;
                }
                else
                {
                    IsShowSubordinateEnable = true;
                }
                SetProperty(ref _selectedTown, value);
            }
        }
        private KeyValuePair<string, string> _selectedIndicator;

        public KeyValuePair<string, string> SelectedIndicator
        {
            get { return _selectedIndicator; }
            set { SetProperty(ref _selectedIndicator, value); }
        }
        #endregion
        #region 用户选择的期别

        private ObservableCollection<string> _period = new ObservableCollection<string>();
        public ObservableCollection<string> Period
        {
            get => _period;
            set
            {
                SetProperty(ref _period, value);
            }
        }
        private void SetPeroid()
        {
            if (Global.IntSeasonNow == 1)
            {
                this.Period.Add("本季度");
            }
            else
            {
                this.Period.Add("本季度");
                this.Period.Add("一到本季度");
            }
        }

        private string _selectedPeriod;
        public string SelectedPeriod
        {
            get { return _selectedPeriod; }
            set { SetProperty(ref _selectedPeriod, value); }
        }
        #endregion
        #region MassSheepAndGoatMsgs和SingleIndicatorCowMsgs
        private ObservableCollection<MassSheepAndGoatMsgs> _massSheepAndGoatCln = new ObservableCollection<MassSheepAndGoatMsgs>();

        public ObservableCollection<MassSheepAndGoatMsgs> MassSheepAndGoatCln
        {
            get { return _massSheepAndGoatCln; }
            set { SetProperty(ref _massSheepAndGoatCln, value); }
        }
        private ObservableCollection<SingleIndicatorMsg> _singleIndicatorSheepAndGoatCln = new ObservableCollection<SingleIndicatorMsg>();

        public ObservableCollection<SingleIndicatorMsg> SingleIndicatorSheepAndGoatCln
        {
            get { return _singleIndicatorSheepAndGoatCln; }
            set { _singleIndicatorSheepAndGoatCln = value; }
        }

        #endregion
        SheepAndGoat 羊规模户信息 = new SheepAndGoat();
        Models.ShowMsg showMsg;
        #region 构造函数
        public Page5A211ViewModel()
        {
            //要先进行县乡名称修正
            //需要用到的表:1到本季度的用表         本季度\上季度\同比季度



            showMsg = new Models.ShowMsg();
            //this.ShowAllMsgCommand = new RelayCommand(ShowAllMsgs);
            this.ShowAllMsgCommand = new RelayCommand(async () => await ShowPigMassMsgInDataGridAsync());
            // this.ShowSingleMsgCommand = new RelayCommand(() => ShowSingleMsgOnlyNow());
            this.ShowSingleMsgCommand = new RelayCommand(async () => await ShowSingleIndicatorMsgInDataGridAsync());
            //this.ShowAllOwnerMsgCommand = new RelayCommand(() => ShowAllOwnerMsg());
            this.ShowAllOwnerMsgCommand = new RelayCommand(async () => await ShowMsgByAllFarmersInDataGridAsync());
            this.AllExpertToExcelCommand = new RelayCommand(async () => await ExpertToExcelAll());
            this.SingleExpertToExcelCommand = new RelayCommand(async () => await ExpertToExcelSingle());
            this.QueryFrom1ToNewTableInDBCommand = new RelayCommand(() => ShowFrom1ToNowTablesInDB());
            SetPeroid();
            MySimpleFactory factory = new MySimpleFactory();


            try
            {
                MyDBOperator myDbOperator = MyDBOperator.DBOperatorInstance;

                if (myDbOperator.CheckIfTableExists(withYSTableNameNow) == false)
                {
                    CityCln.Clear();
                    CityCln.Add(new KeyValuePair<string, string>("0", $"未导入{withYSTableNameNow},或导入错误"));
                    return;
                }
                if (!myDbOperator.GetColumnNamesArr(myDbOperator.GetDataTable($"select * from {withYSTableNameNow}")).Contains("已修正"))
                {
                    Modify.ModifyCityCountryTownOfOneTable(withYSTableNameNow);
                }
                CityCln = showMsg.GetAllCityItems(withYSTableNameNow);
                IndicatorCln = showMsg.GetAllIndicators("羊", withYSTableNameNow);
            }
            catch (Exception ex)
            {
                StaticLogger.SystemError(ex.Message);
                MyMessagebox.Show($"出现错误,错误内容为:{ex.Message}");
            }
        }



        #endregion
        #region 临时增加的
        private DataTable GetTtableMsgs(string[] tables)
        {
            MyDBOperator getTableMsgOperator = MyDBOperator.DBOperatorInstance;
            DataTable DTOfTableMsgs = getTableMsgOperator.GetDataTable("select 表名称,导入时间,已导入的户数 from DataTable要用的字段 "); //手动创建的datatable显示不了,这里是获取标题
            // DTOfTableMsgs = new DataTable();
            //DTOfTableMsgs.Columns.Add("表名", typeof(string));
            //DTOfTableMsgs.Columns.Add("导入时间", typeof(string));
            foreach (string tablename in tables)
            {
                int rowcount;
                string TableCreateTime = getTableMsgOperator.ReturnTableCreateTimeAndRowCount(tablename, out rowcount);
                DataRow tableMsgRow = DTOfTableMsgs.NewRow();
                tableMsgRow[0] = tablename;
                tableMsgRow[1] = TableCreateTime;
                tableMsgRow[2] = rowcount;
                DTOfTableMsgs.Rows.Add(tableMsgRow);
            }

            //DTOfTableMsgs.Columns[0].ColumnName = "表名";
            //DTOfTableMsgs.Columns[1].ColumnName = "是否导入";
            return DTOfTableMsgs;
        }

        private void ShowFrom1ToNowTablesInDB()
        {
            string[] allNeedTables = null;
            if (Global.IntSeasonNow == 1)
            {
                return;
            }
            if (Global.IntSeasonNow == 2)
            {
                allNeedTables = new string[] { withYSTableNameNow, withYSTableNamePre };
            }
            else if (Global.IntSeasonNow == 3)
            {
                allNeedTables = new string[] { withYSTableNameNow, withYSTableNamePre, withYSTableNameSpring };
            }
            else if (Global.IntSeasonNow == 4)
            {
                allNeedTables = new string[] {withYSTableNameNow, withYSTableNamePre, withYSTableNameSummer, withYSTableNameSpring };
            }
            //检查这几张表的情况,并在单独一个窗口中显示
            DataTable dtOfTableMsgs = GetTtableMsgs(allNeedTables);
            if (dtOfTableMsgs != null && dtOfTableMsgs.Rows.Count > 0)
            {
                FromSpringToNowTableMsgs window = new FromSpringToNowTableMsgs(dtOfTableMsgs);

                window.Show();
            }
        }
        #endregion
        private async Task ShowSingleIndicatorMsgInDataGridAsync()
        {
            try
            {
                string nameInDTOfSingleIndicator = null;
                string pieTitleName = null;
                if (SelectedCity.Value == null || SelectedCountry.Value == null || SelectedTown.Value == null)
                {
                    await MyMessagebox.ShowAsyncInMainWindow("市、县、乡镇至少有一个下拉框未选择内容!", Icon.Error);
                    return;
                }
                if (string.IsNullOrEmpty(SelectedIndicator.Value))
                {
                    await MyMessagebox.ShowAsyncInMainWindow("未选择具体指标!", Icon.Error);
                    return;
                }
                //先判断一下是否有表缺失
                string[] allNeedTables = new string[] { withYSTableNameNow, withYSTableNamePre, withYSTableNameYOY };
                var myDBOperator = MyDBOperator.DBOperatorInstance;
                var notExistTables = myDBOperator.CheckTablesExist(allNeedTables);
                string allNotExistTablesJoin = null;
                if (notExistTables.Count > 0)
                {
                    for (int i = 0; i < notExistTables.Count; i++)
                    {
                        //allNotExistTablesJoin += $"、{notExistTables[i].Substring(3)}";
                        allNotExistTablesJoin += $"、{notExistTables[i]}";
                    }
                    allNotExistTablesJoin = allNotExistTablesJoin.Substring(1);
                    await MyMessagebox.ShowAsyncInMainWindow(allNotExistTablesJoin + "不存在,请先导入", Icon.Warning);
                    return;
                }
                //修正一下 
                MyProgressBar _progressBar = new MyProgressBar();
                _progressBar.Show();
                var progress = new Progress<int>(value => _progressBar.UpdateProgress(value));
                string modifyOutcome = await Task.Run(() => Modify.ModifyTables(allNeedTables, progress));
                _progressBar.Close();
                if (modifyOutcome != null)
                {
                    await MyMessagebox.ShowAsyncInMainWindow($"市县乡名称修正时出错,错误内容为:{modifyOutcome},一般不影响数据展示,建议排查", Icon.Warning);
                }
                dtOfSingleIndicator = 羊规模户信息.GetDTSingleIndicatorOfSubordinateThisSeason(SelectedCity, SelectedCountry, SelectedTown, SelectedIndicator);
                if (dtOfSingleIndicator != null)
                {
                    SingleIndicatorSheepAndGoatCln.Clear();
                    for (int i = 0; i < dtOfSingleIndicator.Rows.Count; i++)
                    {
                        DataRow row = dtOfSingleIndicator.Rows[i];
                        // 使用 Convert.IsDBNull 检查并提供默认值
                        var 地区名称 = row.Field<string>(0) ?? "未知";
                        var 地区编码 = row.Field<string>(1) ?? "未知";

                        // 使用 Convert.ToDouble 并处理可能的 null 值
                        var 当季指标值 = Convert.ToDouble(row.IsNull(2) ? 0 : row[2]);
                        var 上季指标值 = Convert.ToDouble(row.IsNull(3) ? 0 : row[3]);
                        var 同比季度指标值 = Convert.ToDouble(row.IsNull(4) ? 0 : row[4]);
                        var 环比增量 = Convert.ToDouble(row.IsNull(5) ? 0 : row[5]);
                        var 同比增量 = Convert.ToDouble(row.IsNull(6) ? 0 : row[6]);
                        var 环比增幅 = Convert.ToDouble(row.IsNull(7) ? 0 : row[7]);
                        var 同比增幅 = Convert.ToDouble(row.IsNull(8) ? 0 : row[8]);
                        var 当季占比 = Convert.ToDouble(row.IsNull(9) ? 0 : row[9]);
                        var 上季占比 = Convert.ToDouble(row.IsNull(10) ? 0 : row[10]);
                        var 同比季度占比 = Convert.ToDouble(row.IsNull(11) ? 0 : row[11]);
                        SingleIndicatorSheepAndGoatCln.Add(new SingleIndicatorMsg(地区名称, 地区编码, 当季指标值, 上季指标值,
                            同比季度指标值, 环比增量, 同比增量, 环比增幅, 同比增幅, 当季占比, 上季占比, 同比季度占比));
                    }
                    //没有缺失的话要先市县乡名称修正一下
                    if (SelectedCity.Value.Equals("全部"))//全省的
                    {


                        nameInDTOfSingleIndicator = "市";
                        pieTitleName = "分地市";
                    }
                    else if (SelectedCity.Value.Equals("全部") == false && SelectedCountry.Value.Equals("全部") == true)//某个市的
                    {


                        nameInDTOfSingleIndicator = "县";
                        pieTitleName = "分县";
                    }
                    else if (SelectedCity.Value.Equals("全部") == false && SelectedCountry.Value.Equals("全部") == false && SelectedTown.Value.Equals("全部") == true)//某个县的
                    {

                        nameInDTOfSingleIndicator = "乡";
                        pieTitleName = "分乡镇";
                    }
                    else if (SelectedCity.Value.Equals("全部") == false && SelectedCountry.Value.Equals("全部") == false && SelectedTown.Value.Equals("全部") == false)//某个乡镇的
                    {
                        nameInDTOfSingleIndicator = "户主姓名";
                        pieTitleName = "分户";
                    }
                    if (dtOfSingleIndicator != null)
                    {
                        List<ISeries> circleConsistOfPiecesOfIndicatorSeasonNowConvertToArrayFinally = new List<ISeries>();
                        List<ISeries> circleConsistOfPiecesOfIndicatorSeasonPreConvertToArrayFinally = new List<ISeries>();
                        List<ISeries> circleConsistOfPiecesOfIndicatorSeasonYOYConvertToArrayFinally = new List<ISeries>();
                        List<double> listColumnSeriesValuesIncreaseOfPreConvertToArrayLast = new List<double>();
                        List<double> listColumnSeriesValuesIncreaseOfYOYConvertToArrayLast = new List<double>();
                        List<double> listLineSeriesValuesIncreaseOfPreConvertToArrayLast = new List<double>();
                        List<double> listLineSeriesValuesIncreaseOfYOYConvertToArrayLast = new List<double>();
                        List<string> listColumnsXAxisNames = new List<string>();
                        List<string> listLineXAxisNames = new List<string>();
                        for (int i = 0; i < dtOfSingleIndicator.Rows.Count - 1; i++)
                        {
                            DataRow row = dtOfSingleIndicator.Rows[i];
                            AddOnePieSeriesToCircleList(row, nameInDTOfSingleIndicator, $"本季度{SelectedIndicator.Value}", ref circleConsistOfPiecesOfIndicatorSeasonNowConvertToArrayFinally);
                            AddOnePieSeriesToCircleList(row, nameInDTOfSingleIndicator, $"上季度{SelectedIndicator.Value}", ref circleConsistOfPiecesOfIndicatorSeasonPreConvertToArrayFinally);
                            AddOnePieSeriesToCircleList(row, nameInDTOfSingleIndicator, $"同比季度{SelectedIndicator.Value}", ref circleConsistOfPiecesOfIndicatorSeasonYOYConvertToArrayFinally);
                            AddTw0TotalColumsToCart(row, 0, "环比增量", "同比增量", ref listColumnSeriesValuesIncreaseOfPreConvertToArrayLast, ref listColumnSeriesValuesIncreaseOfYOYConvertToArrayLast, ref listColumnsXAxisNames);
                            AddTw0TotalColumsToCart(row, 0, "环比增幅(%)", "同比增幅(%)", ref listLineSeriesValuesIncreaseOfPreConvertToArrayLast, ref listLineSeriesValuesIncreaseOfYOYConvertToArrayLast, ref listLineXAxisNames);
                        }
                        if (circleConsistOfPiecesOfIndicatorSeasonNowConvertToArrayFinally.Count > 0)
                        {
                            PieSeriesOfNow = circleConsistOfPiecesOfIndicatorSeasonNowConvertToArrayFinally.ToArray();
                            TitleOfPieSeriesNow = SetTitleOfPie($"规模户{SelectedIndicator.Value}{pieTitleName}本期占比情况示意图");
                        }
                        else
                        {
                            PieSeriesOfNow = SetNullPieSeries();
                            TitleOfPieSeriesNow = SetTitleOfPie($"规模户{SelectedIndicator.Value}{pieTitleName}本期无数据");
                        }
                        if (circleConsistOfPiecesOfIndicatorSeasonPreConvertToArrayFinally.Count > 0)//如果上季度的数据不为空,就显示到界面上
                        {
                            PieSeriesOfPre = circleConsistOfPiecesOfIndicatorSeasonPreConvertToArrayFinally.ToArray();
                            //TitleOfPieSeriesPre = base.SetTitleOfPie("规模户上季度" + this.SelectedIndicator.Value + PieTitleName + "数据和比重示意图(%)");
                            TitleOfPieSeriesPre = base.SetTitleOfPie("规模户" + this.SelectedIndicator.Value + pieTitleName + "上期占比情况示意图");
                        }
                        else
                        {
                            PieSeriesOfPre = SetNullPieSeries();
                            TitleOfPieSeriesPre = SetTitleOfPie($"规模户{SelectedIndicator.Value}{pieTitleName}上期无数据"); ;
                        }
                        if (circleConsistOfPiecesOfIndicatorSeasonYOYConvertToArrayFinally.Count > 0)//如果同比季度的数据不为空,就显示到界面上
                        {
                            PieSeriesOfYOY = circleConsistOfPiecesOfIndicatorSeasonYOYConvertToArrayFinally.ToArray();
                            // TitleOfPieSeriesYOY = SetTitleOfPie("规模户同比季度" + this.SelectedIndicator.Value + PieTitleName + "数据和比重示意图(%)");
                            TitleOfPieSeriesYOY = SetTitleOfPie($"规模户{SelectedIndicator.Value}{pieTitleName}去年同期占比图");
                        }
                        else
                        {
                            PieSeriesOfYOY = SetNullPieSeries();
                            TitleOfPieSeriesYOY = SetTitleOfPie($"规模户{SelectedIndicator.Value}{pieTitleName}去年同期无数据");
                        }


                        if (listColumnSeriesValuesIncreaseOfPreConvertToArrayLast.Count > 0)
                        {
                            //直方图标题
                            TitleOfCartNow = GetTitleOfCart($"规模户{SelectedIndicator.Value}环比和同比增量数据");
                            CartSeriesNow = SetColumnsSeries<double, double>($"{SelectedIndicator.Value}环比增量", listColumnSeriesValuesIncreaseOfPreConvertToArrayLast,
                               $"{SelectedIndicator.Value}同比增量", listColumnSeriesValuesIncreaseOfYOYConvertToArrayLast);
                            CartXAxisOfNow = SetColumnsXAxis($"{pieTitleName}名称", listColumnsXAxisNames);
                            CartYAxisOfNow = SetColumnsYAxis(SelectedIndicator.Value + "增量数据");
                        }
                        else
                        {
                            TitleOfCartNow = GetTitleOfCart($"规模户{SelectedIndicator.Value}环比和同比增量数据无数据");
                            CartXAxisOfNow = SetNullXAxis();
                            CartYAxisOfNow = SetNullYAxis();
                            CartSeriesNow = SetNullColumnsSeries();
                        }
                        if (listLineSeriesValuesIncreaseOfPreConvertToArrayLast.Count > 0)
                        {
                            LineTitle = base.SetTitleOfLine("规模户" + this.SelectedIndicator.Value + pieTitleName + "增幅图(%)");
                            LineSeries = base.SetLineSeries<double, double>(SelectedIndicator.Value + "环比增幅", listLineSeriesValuesIncreaseOfPreConvertToArrayLast,
                                SelectedIndicator.Value + "同比增幅", listLineSeriesValuesIncreaseOfYOYConvertToArrayLast);
                            LineXAxis = SetLineXAxis(pieTitleName + "名称", listLineXAxisNames);
                            LineYAxis = SetLineYAxis(SelectedIndicator.Value + "增幅数据");
                        }

                        else
                        {
                            LineTitle = SetTitleOfLine("规模户" + this.SelectedIndicator.Value + pieTitleName + "增幅无数据"); ;
                            LineXAxis = SetNullXAxis(); ;
                            LineYAxis = SetNullYAxis();
                            LineSeries = SetNullLineSeries();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                StaticLogger.SystemError($"出现错误,错误信息{ex.Message},{Environment.NewLine}堆栈为{ex.StackTrace},{Environment.NewLine}对象为{ex.Source},{Environment.NewLine}方法为{ex.TargetSite}");
                await MyMessagebox.ShowAsyncInMainWindow($"出现错误,错误内容为{ex.Message}", Icon.Error);
            }
        }





        private async Task ShowPigMassMsgInDataGridAsync()
        {
            try
            {

                if (SelectedCity.Value == null || SelectedCountry.Value == null || SelectedTown.Value == null)
                {
                    await MyMessagebox.ShowAsyncInMainWindow("市、县、乡镇至少有一个下拉框未选择内容!", Icon.Error);
                    return;
                }
                if (this.SelectedPeriod == null)
                {
                    await MyMessagebox.ShowAsyncInMainWindow("请先选择要展示的时期!", Icon.Error);
                    return;
                }
                else if (this.SelectedPeriod.Equals("本季度"))
                {
                    await ShowPigMassMsgInDataGridThisSeasonAsync();
                }
                else//如果用户点击的是"1到本季度"
                {
                    //先看一下一季度到现在的表是否有缺失
                    if (Global.IntSeasonNow == 1)
                    {
                        await ShowPigMassMsgInDataGridThisSeasonAsync();
                    }
                    else if (Global.IntSeasonNow == 2)//如果是二季度的话
                    {
                        //先判断一下表是否存在
                        string[] allNeedTables = new string[] { withYSTableNameNow, withYSTableNamePre };
                        var myDBOperator = MyDBOperator.DBOperatorInstance;
                        var notExistTables = myDBOperator.CheckTablesExist(allNeedTables);
                        string allNotExistTablesJoin = null;
                        if (notExistTables.Count > 0)
                        {
                            for (int i = 0; i < notExistTables.Count; i++)
                            {
                                allNotExistTablesJoin += $"、{notExistTables[i].Substring(3)}";
                            }
                            allNotExistTablesJoin = allNotExistTablesJoin.Substring(1);
                            await MyMessagebox.ShowAsyncInMainWindow(allNotExistTablesJoin + "不存在,请先导入", Icon.Warning);
                            return;
                        }

                        //如果没有缺失,需要先进行修正再进行合并
                        MyProgressBar _progressBar = new MyProgressBar();
                        _progressBar.Show();
                        var progress = new Progress<int>(value => _progressBar.UpdateProgress(value));
                        string modifyOutcome = await Task.Run(() => Modify.ModifyTables(allNeedTables, progress));
                        _progressBar.Close();
                        if (modifyOutcome != null)
                        {
                            await MyMessagebox.ShowAsyncInMainWindow($"市县乡名称修正时出错,错误内容为:{modifyOutcome},一般不影响数据展示,建议排查", Icon.Warning);
                        }
                        await ShowPigMassMsgInDataGridThisYearAsync(allNeedTables, 2);
                    }
                    else if (Global.IntSeasonNow == 3)
                    {
                        string[] allNeedTables = new string[] { withYSTableNameNow, withYSTableNamePre, withYSTableNameSpring };
                        var myDBOperator = MyDBOperator.DBOperatorInstance;
                        var notExistTables = myDBOperator.CheckTablesExist(allNeedTables);
                        string allNotExistTablesJoin = null;
                        if (notExistTables.Count > 0)
                        {
                            for (int i = 0; i < notExistTables.Count; i++)
                            {
                                allNotExistTablesJoin += $"、{notExistTables[i].Substring(3)}";
                            }
                            allNotExistTablesJoin = allNotExistTablesJoin.Substring(1);
                            await MyMessagebox.ShowAsyncInMainWindow(allNotExistTablesJoin + "不存在,请先导入", Icon.Warning);
                            return;
                        }
                        //如果没有缺失,需要先进行修正再进行合并
                        MyProgressBar _progressBar = new MyProgressBar();
                        _progressBar.Show();
                        var progress = new Progress<int>(value => _progressBar.UpdateProgress(value));
                        string modifyOutcome = await Task.Run(() => Modify.ModifyTables(allNeedTables, progress));
                        _progressBar.Close();
                        if (modifyOutcome != null)
                        {
                            await MyMessagebox.ShowAsyncInMainWindow($"市县乡名称修正时出错,错误内容为:{modifyOutcome},一般不影响数据展示,建议排查", Icon.Warning);
                        }
                        await ShowPigMassMsgInDataGridThisYearAsync(allNeedTables, 3);
                    }
                    else if (Global.IntSeasonNow == 4)
                    {
                        string[] allNeedTables = new string[] { withYSTableNameNow, withYSTableNamePre, withYSTableNameSpring, withYSTableNameSummer };
                        var myDBOperator = MyDBOperator.DBOperatorInstance;
                        var notExistTables = myDBOperator.CheckTablesExist(allNeedTables);
                        string allNotExistTablesJoin = null;
                        if (notExistTables.Count > 0)
                        {
                            for (int i = 0; i < notExistTables.Count; i++)
                            {
                                allNotExistTablesJoin += $"、{notExistTables[i].Substring(3)}";
                            }
                            allNotExistTablesJoin = allNotExistTablesJoin.Substring(1);
                            await MyMessagebox.ShowAsyncInMainWindow(allNotExistTablesJoin + "不存在,请先导入", Icon.Warning);
                            return;
                        }
                        //如果没有缺失,需要先进行修正再进行合并
                        MyProgressBar _progressBar = new MyProgressBar();
                        _progressBar.Show();
                        var progress = new Progress<int>(value => _progressBar.UpdateProgress(value));
                        string modifyOutcome = await Task.Run(() => Modify.ModifyTables(allNeedTables, progress));
                        _progressBar.Close();
                        if (modifyOutcome != null)
                        {
                            await MyMessagebox.ShowAsyncInMainWindow($"市县乡名称修正时出错,错误内容为:{modifyOutcome},一般不影响数据展示,建议排查", Icon.Warning);
                        }
                        await ShowPigMassMsgInDataGridThisYearAsync(allNeedTables, 4);
                    }
                }
            }
            catch (Exception ex)
            {
                StaticLogger.SystemError($"出现错误,错误信息{ex.Message},{Environment.NewLine}堆栈为{ex.StackTrace},{Environment.NewLine}对象为{ex.Source},{Environment.NewLine}方法为{ex.TargetSite}");
                await MyMessagebox.ShowAsyncInMainWindow($"出现错误,错误内容为{ex.Message}", Icon.Error);
            }
        }

        private async Task ShowPigMassMsgInDataGridThisYearAsync(string[] tableNames, int 期别季)
        {
            try
            {
                MassSheepAndGoatCln.Clear();

                DTAll = 羊规模户信息.GetDTAllIndicatorsOfSubordinateThisYear(SelectedCity, SelectedCountry, SelectedTown, tableNames, 期别季);
                if (DTAll != null)
                {
                    if (SelectedCity.Value.Equals("全部"))//省
                    {
                        for (int i = 0; i < DTAll.Rows.Count; i++)
                        {

                            DataRow row = DTAll.Rows[i];
                            var 市名 = row[0].ToString();
                            var 县名 = "-";
                            var 乡名 = "-";
                            var 户主姓名 = "-";
                            var 养殖户类型 = "-";
                            var 当季出栏数 = Convert.ToDouble(row.IsNull(2) ? 0 : row[2]);
                            var 期末存栏合计 = Convert.ToDouble(row.IsNull(3) ? 0 : row[3]);
                            var 山羊存栏 = Convert.ToDouble(row.IsNull(4) ? 0 : row[4]);
                            var 能繁殖母山羊存栏 = Convert.ToDouble(row.IsNull(5) ? 0 : row[5]);
                            var 绵羊存栏 = Convert.ToDouble(row.IsNull(6) ? 0 : row[6]);
                            var 能繁殖母绵羊存栏 = Convert.ToDouble(row.IsNull(7) ? 0 : row[7]);
                            var 期内增加 = Convert.ToDouble(row.IsNull(8) ? 0 : row[8]);
                            var 自繁 = Convert.ToDouble(row.IsNull(9) ? 0 : row[9]);
                            var 山羊自繁 = Convert.ToDouble(row.IsNull(10) ? 0 : row[10]);
                            var 绵羊自繁 = Convert.ToDouble(row.IsNull(11) ? 0 : row[11]);
                            var 购进 = Convert.ToDouble(row.IsNull(12) ? 0 : row[12]);
                            var 山羊购进 = Convert.ToDouble(row.IsNull(13) ? 0 : row[13]);

                            var 绵羊购进 = Convert.ToDouble(row.IsNull(14) ? 0 : row[14]);
                            var 期内减少 = Convert.ToDouble(row.IsNull(15) ? 0 : row[15]);
                            var 自宰 = Convert.ToDouble(row.IsNull(16) ? 0 : row[16]);
                            var 出售 = Convert.ToDouble(row.IsNull(17) ? 0 : row[17]);
                            var 出售羊金额 = Convert.ToDouble(row.IsNull(18) ? 0 : row[18]);
                            var 出售羊重量 = Convert.ToDouble(row.IsNull(19) ? 0 : row[19]);
                            var 当季绵羊毛产量 = Convert.ToDouble(row.IsNull(20) ? 0 : row[20]);
                            var 当季山羊绒产量 = Convert.ToDouble(row.IsNull(21) ? 0 : row[21]);

                            MassSheepAndGoatMsgs MassSheepAndGoat = new MassSheepAndGoatMsgs(
                                市名, 县名, 乡名, 户主姓名, 养殖户类型, 当季出栏数, 期末存栏合计, 山羊存栏, 能繁殖母山羊存栏, 绵羊存栏, 能繁殖母绵羊存栏, 期内增加, 自繁, 山羊自繁, 绵羊自繁,
                                购进, 山羊购进, 绵羊购进, 期内减少, 自宰, 出售, 出售羊金额, 出售羊重量, 当季绵羊毛产量, 当季山羊绒产量);
                            MassSheepAndGoatCln.Add(MassSheepAndGoat);
                        }

                    }
                    else if (SelectedCity.Value.Equals("全部") == false && SelectedCountry.Value.Equals("全部"))//某个市的
                    {
                        for (global::System.Int32 i = 0; i < DTAll.Rows.Count; i++)
                        {

                            DataRow row = DTAll.Rows[i];
                            var 市名 = "-";
                            var 县名 = row[0].ToString();
                            var 乡名 = "-";
                            var 户主姓名 = "-";
                            var 养殖户类型 = "-";
                            var 当季出栏数 = Convert.ToDouble(row.IsNull(2) ? 0 : row[2]);
                            var 期末存栏合计 = Convert.ToDouble(row.IsNull(3) ? 0 : row[3]);
                            var 山羊存栏 = Convert.ToDouble(row.IsNull(4) ? 0 : row[4]);
                            var 能繁殖母山羊存栏 = Convert.ToDouble(row.IsNull(5) ? 0 : row[5]);
                            var 绵羊存栏 = Convert.ToDouble(row.IsNull(6) ? 0 : row[6]);
                            var 能繁殖母绵羊存栏 = Convert.ToDouble(row.IsNull(7) ? 0 : row[7]);
                            var 期内增加 = Convert.ToDouble(row.IsNull(8) ? 0 : row[8]);
                            var 自繁 = Convert.ToDouble(row.IsNull(9) ? 0 : row[9]);
                            var 山羊自繁 = Convert.ToDouble(row.IsNull(10) ? 0 : row[10]);
                            var 绵羊自繁 = Convert.ToDouble(row.IsNull(11) ? 0 : row[11]);
                            var 购进 = Convert.ToDouble(row.IsNull(12) ? 0 : row[12]);
                            var 山羊购进 = Convert.ToDouble(row.IsNull(13) ? 0 : row[13]);

                            var 绵羊购进 = Convert.ToDouble(row.IsNull(14) ? 0 : row[14]);
                            var 期内减少 = Convert.ToDouble(row.IsNull(15) ? 0 : row[15]);
                            var 自宰 = Convert.ToDouble(row.IsNull(16) ? 0 : row[16]);
                            var 出售 = Convert.ToDouble(row.IsNull(17) ? 0 : row[17]);
                            var 出售羊金额 = Convert.ToDouble(row.IsNull(18) ? 0 : row[18]);
                            var 出售羊重量 = Convert.ToDouble(row.IsNull(19) ? 0 : row[19]);
                            var 当季绵羊毛产量 = Convert.ToDouble(row.IsNull(20) ? 0 : row[20]);
                            var 当季山羊绒产量 = Convert.ToDouble(row.IsNull(21) ? 0 : row[21]);

                            MassSheepAndGoatMsgs MassSheepAndGoat = new MassSheepAndGoatMsgs(
                                市名, 县名, 乡名, 户主姓名, 养殖户类型, 当季出栏数, 期末存栏合计, 山羊存栏, 能繁殖母山羊存栏, 绵羊存栏, 能繁殖母绵羊存栏, 期内增加, 自繁, 山羊自繁, 绵羊自繁,
                                购进, 山羊购进, 绵羊购进, 期内减少, 自宰, 出售, 出售羊金额, 出售羊重量, 当季绵羊毛产量, 当季山羊绒产量);
                            MassSheepAndGoatCln.Add(MassSheepAndGoat);
                        }
                    }
                    else if (SelectedCountry.Equals("全部") == false)//某一个具体县的
                    {

                        for (global::System.Int32 i = 0; i < DTAll.Rows.Count; i++)
                        {

                            DataRow row = DTAll.Rows[i];
                            var 市名 = "-";
                            var 县名 = "-";
                            var 乡名 = row[0].ToString();
                            var 户主姓名 = "-";
                            var 养殖户类型 = "-";
                            var 当季出栏数 = Convert.ToDouble(row.IsNull(2) ? 0 : row[2]);
                            var 期末存栏合计 = Convert.ToDouble(row.IsNull(3) ? 0 : row[3]);
                            var 山羊存栏 = Convert.ToDouble(row.IsNull(4) ? 0 : row[4]);
                            var 能繁殖母山羊存栏 = Convert.ToDouble(row.IsNull(5) ? 0 : row[5]);
                            var 绵羊存栏 = Convert.ToDouble(row.IsNull(6) ? 0 : row[6]);
                            var 能繁殖母绵羊存栏 = Convert.ToDouble(row.IsNull(7) ? 0 : row[7]);
                            var 期内增加 = Convert.ToDouble(row.IsNull(8) ? 0 : row[8]);
                            var 自繁 = Convert.ToDouble(row.IsNull(9) ? 0 : row[9]);
                            var 山羊自繁 = Convert.ToDouble(row.IsNull(10) ? 0 : row[10]);
                            var 绵羊自繁 = Convert.ToDouble(row.IsNull(11) ? 0 : row[11]);
                            var 购进 = Convert.ToDouble(row.IsNull(12) ? 0 : row[12]);
                            var 山羊购进 = Convert.ToDouble(row.IsNull(13) ? 0 : row[13]);

                            var 绵羊购进 = Convert.ToDouble(row.IsNull(14) ? 0 : row[14]);
                            var 期内减少 = Convert.ToDouble(row.IsNull(15) ? 0 : row[15]);
                            var 自宰 = Convert.ToDouble(row.IsNull(16) ? 0 : row[16]);
                            var 出售 = Convert.ToDouble(row.IsNull(17) ? 0 : row[17]);
                            var 出售羊金额 = Convert.ToDouble(row.IsNull(18) ? 0 : row[18]);
                            var 出售羊重量 = Convert.ToDouble(row.IsNull(19) ? 0 : row[19]);
                            var 当季绵羊毛产量 = Convert.ToDouble(row.IsNull(20) ? 0 : row[20]);
                            var 当季山羊绒产量 = Convert.ToDouble(row.IsNull(21) ? 0 : row[21]);

                            MassSheepAndGoatMsgs MassSheepAndGoat = new MassSheepAndGoatMsgs(
                                市名, 县名, 乡名, 户主姓名, 养殖户类型, 当季出栏数, 期末存栏合计, 山羊存栏, 能繁殖母山羊存栏, 绵羊存栏, 能繁殖母绵羊存栏, 期内增加, 自繁, 山羊自繁, 绵羊自繁,
                                购进, 山羊购进, 绵羊购进, 期内减少, 自宰, 出售, 出售羊金额, 出售羊重量, 当季绵羊毛产量, 当季山羊绒产量);
                            MassSheepAndGoatCln.Add(MassSheepAndGoat);
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                StaticLogger.SystemError($"出现错误,错误信息{ex.Message},{Environment.NewLine}堆栈为{ex.StackTrace},{Environment.NewLine}对象为{ex.Source},{Environment.NewLine}方法为{ex.TargetSite}");
                await MyMessagebox.ShowAsyncInMainWindow($"出现错误,错误内容为{ex.Message}", Icon.Error);
            }
        }
        private async Task ShowPigMassMsgInDataGridThisSeasonAsync()
        {
            try
            {
                string[] allNeedTables = { withYSTableNameNow };
                MyProgressBar _progressBar = new MyProgressBar();
                _progressBar.Show();
                var progress = new Progress<int>(value => _progressBar.UpdateProgress(value));
                string modifyOutcome = await Task.Run(() => Modify.ModifyTables(allNeedTables, progress));
                _progressBar.Close();
                if (modifyOutcome != null)
                {
                    await MyMessagebox.ShowAsyncInMainWindow($"市县乡名称修正时出错,错误内容为:{modifyOutcome},一般不影响数据展示,建议排查", Icon.Warning);
                }
                MassSheepAndGoatCln.Clear();
                DTAll = 羊规模户信息.GetDTAllIndicatorsOfSubordinateThisSeason(SelectedCity, SelectedCountry, SelectedTown);
                if (DTAll == null) return;
                if (SelectedCity.Value.Equals("全部"))//省
                {


                    for (global::System.Int32 i = 0; i < DTAll.Rows.Count; i++)
                    {
                        DataRow row = DTAll.Rows[i];
                        var 市名 = row[0].ToString(); ;
                        var 县名 = "-";
                        var 乡名 = "-";
                        var 户主姓名 = "-";
                        var 养殖户类型 = "-";
                        var 当季出栏数 = Convert.ToDouble(row.IsNull(2) ? 0 : row[2]);
                        var 期末存栏合计 = Convert.ToDouble(row.IsNull(3) ? 0 : row[3]);
                        var 山羊存栏 = Convert.ToDouble(row.IsNull(4) ? 0 : row[4]);
                        var 能繁殖母山羊存栏 = Convert.ToDouble(row.IsNull(5) ? 0 : row[5]);
                        var 绵羊存栏 = Convert.ToDouble(row.IsNull(6) ? 0 : row[6]);
                        var 能繁殖母绵羊存栏 = Convert.ToDouble(row.IsNull(7) ? 0 : row[7]);
                        var 期内增加 = Convert.ToDouble(row.IsNull(8) ? 0 : row[8]);
                        var 自繁 = Convert.ToDouble(row.IsNull(9) ? 0 : row[9]);
                        var 山羊自繁 = Convert.ToDouble(row.IsNull(10) ? 0 : row[10]);
                        var 绵羊自繁 = Convert.ToDouble(row.IsNull(11) ? 0 : row[11]);
                        var 购进 = Convert.ToDouble(row.IsNull(12) ? 0 : row[12]);
                        var 山羊购进 = Convert.ToDouble(row.IsNull(13) ? 0 : row[13]);

                        var 绵羊购进 = Convert.ToDouble(row.IsNull(14) ? 0 : row[14]);
                        var 期内减少 = Convert.ToDouble(row.IsNull(15) ? 0 : row[15]);
                        var 自宰 = Convert.ToDouble(row.IsNull(16) ? 0 : row[16]);
                        var 出售 = Convert.ToDouble(row.IsNull(17) ? 0 : row[17]);
                        var 出售羊金额 = Convert.ToDouble(row.IsNull(18) ? 0 : row[18]);
                        var 出售羊重量 = Convert.ToDouble(row.IsNull(19) ? 0 : row[19]);
                        var 当季绵羊毛产量 = Convert.ToDouble(row.IsNull(20) ? 0 : row[20]);
                        var 当季山羊绒产量 = Convert.ToDouble(row.IsNull(21) ? 0 : row[21]);

                        MassSheepAndGoatMsgs MassSheepAndGoat = new MassSheepAndGoatMsgs(
                            市名, 县名, 乡名, 户主姓名, 养殖户类型, 当季出栏数, 期末存栏合计, 山羊存栏, 能繁殖母山羊存栏, 绵羊存栏, 能繁殖母绵羊存栏, 期内增加, 自繁, 山羊自繁, 绵羊自繁,
                            购进, 山羊购进, 绵羊购进, 期内减少, 自宰, 出售, 出售羊金额, 出售羊重量, 当季绵羊毛产量, 当季山羊绒产量);
                        MassSheepAndGoatCln.Add(MassSheepAndGoat);
                    }
                }
                else if (SelectedCity.Value.Equals("全部") == false && SelectedCountry.Value.Equals("全部"))//某个市的
                {


                    for (global::System.Int32 i = 0; i < DTAll.Rows.Count; i++)
                    {
                        DataRow row = DTAll.Rows[i];
                        var 市名 = "-";
                        var 县名 = row[0].ToString();
                        var 乡名 = "-";
                        var 户主姓名 = "-";
                        var 养殖户类型 = "-";
                        var 当季出栏数 = Convert.ToDouble(row.IsNull(2) ? 0 : row[2]);
                        var 期末存栏合计 = Convert.ToDouble(row.IsNull(3) ? 0 : row[3]);
                        var 山羊存栏 = Convert.ToDouble(row.IsNull(4) ? 0 : row[4]);
                        var 能繁殖母山羊存栏 = Convert.ToDouble(row.IsNull(5) ? 0 : row[5]);
                        var 绵羊存栏 = Convert.ToDouble(row.IsNull(6) ? 0 : row[6]);
                        var 能繁殖母绵羊存栏 = Convert.ToDouble(row.IsNull(7) ? 0 : row[7]);
                        var 期内增加 = Convert.ToDouble(row.IsNull(8) ? 0 : row[8]);
                        var 自繁 = Convert.ToDouble(row.IsNull(9) ? 0 : row[9]);
                        var 山羊自繁 = Convert.ToDouble(row.IsNull(10) ? 0 : row[10]);
                        var 绵羊自繁 = Convert.ToDouble(row.IsNull(11) ? 0 : row[11]);
                        var 购进 = Convert.ToDouble(row.IsNull(12) ? 0 : row[12]);
                        var 山羊购进 = Convert.ToDouble(row.IsNull(13) ? 0 : row[13]);

                        var 绵羊购进 = Convert.ToDouble(row.IsNull(14) ? 0 : row[14]);
                        var 期内减少 = Convert.ToDouble(row.IsNull(15) ? 0 : row[15]);
                        var 自宰 = Convert.ToDouble(row.IsNull(16) ? 0 : row[16]);
                        var 出售 = Convert.ToDouble(row.IsNull(17) ? 0 : row[17]);
                        var 出售羊金额 = Convert.ToDouble(row.IsNull(18) ? 0 : row[18]);
                        var 出售羊重量 = Convert.ToDouble(row.IsNull(19) ? 0 : row[19]);
                        var 当季绵羊毛产量 = Convert.ToDouble(row.IsNull(20) ? 0 : row[20]);
                        var 当季山羊绒产量 = Convert.ToDouble(row.IsNull(21) ? 0 : row[21]);

                        MassSheepAndGoatMsgs MassSheepAndGoat = new MassSheepAndGoatMsgs(
                            市名, 县名, 乡名, 户主姓名, 养殖户类型, 当季出栏数, 期末存栏合计, 山羊存栏, 能繁殖母山羊存栏, 绵羊存栏, 能繁殖母绵羊存栏, 期内增加, 自繁, 山羊自繁, 绵羊自繁,
                            购进, 山羊购进, 绵羊购进, 期内减少, 自宰, 出售, 出售羊金额, 出售羊重量, 当季绵羊毛产量, 当季山羊绒产量);
                        MassSheepAndGoatCln.Add(MassSheepAndGoat);
                    }
                }
                else if (SelectedCity.Value.Equals("全部") == false && SelectedCountry.Value.Equals("全部") == false && SelectedTown.Value.Equals("全部"))//某个县分乡镇的
                {

                    for (global::System.Int32 i = 0; i < DTAll.Rows.Count; i++)
                    {
                        DataRow row = DTAll.Rows[i];
                        var 市名 = "-";
                        var 县名 = "-";
                        var 乡名 = row[0].ToString();
                        var 户主姓名 = "-";
                        var 养殖户类型 = "-";
                        var 当季出栏数 = Convert.ToDouble(row.IsNull(2) ? 0 : row[2]);
                        var 期末存栏合计 = Convert.ToDouble(row.IsNull(3) ? 0 : row[3]);
                        var 山羊存栏 = Convert.ToDouble(row.IsNull(4) ? 0 : row[4]);
                        var 能繁殖母山羊存栏 = Convert.ToDouble(row.IsNull(5) ? 0 : row[5]);
                        var 绵羊存栏 = Convert.ToDouble(row.IsNull(6) ? 0 : row[6]);
                        var 能繁殖母绵羊存栏 = Convert.ToDouble(row.IsNull(7) ? 0 : row[7]);
                        var 期内增加 = Convert.ToDouble(row.IsNull(8) ? 0 : row[8]);
                        var 自繁 = Convert.ToDouble(row.IsNull(9) ? 0 : row[9]);
                        var 山羊自繁 = Convert.ToDouble(row.IsNull(10) ? 0 : row[10]);
                        var 绵羊自繁 = Convert.ToDouble(row.IsNull(11) ? 0 : row[11]);
                        var 购进 = Convert.ToDouble(row.IsNull(12) ? 0 : row[12]);
                        var 山羊购进 = Convert.ToDouble(row.IsNull(13) ? 0 : row[13]);

                        var 绵羊购进 = Convert.ToDouble(row.IsNull(14) ? 0 : row[14]);
                        var 期内减少 = Convert.ToDouble(row.IsNull(15) ? 0 : row[15]);
                        var 自宰 = Convert.ToDouble(row.IsNull(16) ? 0 : row[16]);
                        var 出售 = Convert.ToDouble(row.IsNull(17) ? 0 : row[17]);
                        var 出售羊金额 = Convert.ToDouble(row.IsNull(18) ? 0 : row[18]);
                        var 出售羊重量 = Convert.ToDouble(row.IsNull(19) ? 0 : row[19]);
                        var 当季绵羊毛产量 = Convert.ToDouble(row.IsNull(20) ? 0 : row[20]);
                        var 当季山羊绒产量 = Convert.ToDouble(row.IsNull(21) ? 0 : row[21]);

                        MassSheepAndGoatMsgs MassSheepAndGoat = new MassSheepAndGoatMsgs(
                            市名, 县名, 乡名, 户主姓名, 养殖户类型, 当季出栏数, 期末存栏合计, 山羊存栏, 能繁殖母山羊存栏, 绵羊存栏, 能繁殖母绵羊存栏, 期内增加, 自繁, 山羊自繁, 绵羊自繁,
                            购进, 山羊购进, 绵羊购进, 期内减少, 自宰, 出售, 出售羊金额, 出售羊重量, 当季绵羊毛产量, 当季山羊绒产量);
                        MassSheepAndGoatCln.Add(MassSheepAndGoat);
                    }
                }
            }
            catch (Exception ex)
            {
                StaticLogger.SystemError($"出现错误,错误信息{ex.Message},{Environment.NewLine}堆栈为{ex.StackTrace},{Environment.NewLine}对象为{ex.Source},{Environment.NewLine}方法为{ex.TargetSite}");
                await MyMessagebox.ShowAsyncInMainWindow($"出现错误,错误内容为{ex.Message}", Icon.Error);
            }

            }
        public async Task ShowMsgByAllFarmersInDataGridAsync()
        {
            try
            {
                if (SelectedCity.Value == null || SelectedCountry.Value == null || SelectedTown.Value == null)
                {
                    await MyMessagebox.ShowAsyncInMainWindow("市、县、乡镇至少有一个下拉框未选择内容!", Icon.Error);
                    return;
                }
                if (this.SelectedPeriod == null)
                {
                    await MyMessagebox.ShowAsyncInMainWindow("请先选择要展示的时期!", Icon.Error);
                    return;
                }
                else if (this.SelectedPeriod.Equals("本季度"))
                {
                    await ShowMsgByAllFarmerInDataGridThisSeasonAsync();
                }
                else//如果用户点击的是"1到本季度"
                {
                    //先看一下一季度到现在的表是否有缺失
                    if (Global.IntSeasonNow == 1)
                    {
                        await ShowMsgByAllFarmerInDataGridThisSeasonAsync();
                    }
                    else if (Global.IntSeasonNow == 2)//如果是二季度的话
                    {
                        //先判断一下表是否存在
                        string[] allNeedTables = new string[] { withYSTableNameNow, withYSTableNamePre };
                        var myDBOperator = MyDBOperator.DBOperatorInstance;
                        var notExistTables = myDBOperator.CheckTablesExist(allNeedTables);
                        string allNotExistTablesJoin = null;
                        if (notExistTables.Count > 0)
                        {
                            for (int i = 0; i < notExistTables.Count; i++)
                            {
                                allNotExistTablesJoin += $"、{notExistTables[i].Substring(3)}";
                            }
                            allNotExistTablesJoin = allNotExistTablesJoin.Substring(1);
                            await MyMessagebox.ShowAsyncInMainWindow(allNotExistTablesJoin + "不存在,请先导入", Icon.Warning);
                            return;
                        }
                        //如果没有缺失,需要进行合并
                        MyProgressBar _progressBar = new MyProgressBar();
                        _progressBar.Show();
                        var progress = new Progress<int>(value => _progressBar.UpdateProgress(value));
                        string modifyOutcome = await Task.Run(() => Modify.ModifyTables(allNeedTables, progress));
                        _progressBar.Close();
                        if (modifyOutcome != null)
                        {
                            await MyMessagebox.ShowAsyncInMainWindow($"市县乡名称修正时出错,错误内容为:{modifyOutcome},一般不影响数据展示,建议排查", Icon.Warning);
                        }
                        //如果没有缺失,需要进行合并
                        await ShowMsgByAllFarmerInDataGridThisYearAsync(allNeedTables, 2);
                    }
                    else if (Global.IntSeasonNow == 3)
                    {
                        string[] allNeedTables = new string[] { withYSTableNameNow, withYSTableNamePre, withYSTableNameSpring };
                        var myDBOperator = MyDBOperator.DBOperatorInstance;
                        var notExistTables = myDBOperator.CheckTablesExist(allNeedTables);
                        string allNotExistTablesJoin = null;
                        if (notExistTables.Count > 0)
                        {
                            for (int i = 0; i < notExistTables.Count; i++)
                            {
                                allNotExistTablesJoin += $"、{notExistTables[i].Substring(3)}";
                            }
                            allNotExistTablesJoin = allNotExistTablesJoin.Substring(1);
                            await MyMessagebox.ShowAsyncInMainWindow(allNotExistTablesJoin + "不存在,请先导入", Icon.Warning);
                            return;
                        }
                        //如果没有缺失,需要进行合并
                        MyProgressBar _progressBar = new MyProgressBar();
                        _progressBar.Show();
                        var progress = new Progress<int>(value => _progressBar.UpdateProgress(value));
                        string modifyOutcome = await Task.Run(() => Modify.ModifyTables(allNeedTables, progress));
                        _progressBar.Close();
                        if (modifyOutcome != null)
                        {
                            await MyMessagebox.ShowAsyncInMainWindow($"市县乡名称修正时出错,错误内容为:{modifyOutcome},一般不影响数据展示,建议排查", Icon.Warning);
                        }
                        await ShowMsgByAllFarmerInDataGridThisYearAsync(allNeedTables, 3);
                    }
                    else if (Global.IntSeasonNow == 4)
                    {
                        string[] allNeedTables = new string[] { withYSTableNameNow, withYSTableNamePre, withYSTableNameSpring, withYSTableNameSummer };
                        var myDBOperator = MyDBOperator.DBOperatorInstance;
                        var notExistTables = myDBOperator.CheckTablesExist(allNeedTables);
                        string allNotExistTablesJoin = null;
                        if (notExistTables.Count > 0)
                        {
                            for (int i = 0; i < notExistTables.Count; i++)
                            {
                                allNotExistTablesJoin += $"、{notExistTables[i].Substring(3)}";
                            }
                            allNotExistTablesJoin = allNotExistTablesJoin.Substring(1);
                            await MyMessagebox.ShowAsyncInMainWindow(allNotExistTablesJoin + "不存在,请先导入", Icon.Warning);
                            return;
                        }
                        //如果没有缺失,需要进行合并
                        MyProgressBar _progressBar = new MyProgressBar();
                        _progressBar.Show();
                        var progress = new Progress<int>(value => _progressBar.UpdateProgress(value));
                        string modifyOutcome = await Task.Run(() => Modify.ModifyTables(allNeedTables, progress));
                        _progressBar.Close();
                        if (modifyOutcome != null)
                        {
                            await MyMessagebox.ShowAsyncInMainWindow($"市县乡名称修正时出错,错误内容为:{modifyOutcome},一般不影响数据展示,建议排查", Icon.Warning);
                        }
                        await ShowMsgByAllFarmerInDataGridThisYearAsync(allNeedTables, 4);
                    }
                }
            }
            catch (Exception ex)
            {
                StaticLogger.SystemError($"出现错误,错误信息{ex.Message},{Environment.NewLine}堆栈为{ex.StackTrace},{Environment.NewLine}对象为{ex.Source},{Environment.NewLine}方法为{ex.TargetSite}");
                await MyMessagebox.ShowAsyncInMainWindow($"出现错误,错误内容为{ex.Message}", Icon.Error);
            }
        }

        private async Task ShowMsgByAllFarmerInDataGridThisYearAsync(string[] tableNames, int 期别季)
        {
            try
            {
                //throw new NotImplementedException();
                MassSheepAndGoatCln.Clear();
                DTAll = 羊规模户信息.GetDTAllIndicatorsOfAllOwnersLocalThisYear(SelectedCity, SelectedCountry, SelectedTown, tableNames, 期别季);
                if (DTAll != null)
                {
                    for (int i = 0; i < DTAll.Rows.Count; i++)
                    {

                        DataRow row = DTAll.Rows[i];
                        var 市名 = row[0].ToString();
                        var 县名 = row[2].ToString();
                        var 乡名 = row[4].ToString();
                        var 户主姓名 = row[7].ToString();
                        var 养殖户类型 = row[8].ToString();
                        var 当季出栏数 = Convert.ToDouble(row.IsNull(9) ? 0 : row[9]);
                        var 期末存栏合计 = Convert.ToDouble(row.IsNull(10) ? 0 : row[10]);
                        var 山羊存栏 = Convert.ToDouble(row.IsNull(11) ? 0 : row[11]);
                        var 能繁殖母山羊存栏 = Convert.ToDouble(row.IsNull(12) ? 0 : row[12]);
                        var 绵羊存栏 = Convert.ToDouble(row.IsNull(13) ? 0 : row[13]);
                        var 能繁殖母绵羊存栏 = Convert.ToDouble(row.IsNull(14) ? 0 : row[14]);
                        var 期内增加 = Convert.ToDouble(row.IsNull(15) ? 0 : row[15]);
                        var 自繁 = Convert.ToDouble(row.IsNull(16) ? 0 : row[16]);
                        var 山羊自繁 = Convert.ToDouble(row.IsNull(17) ? 0 : row[17]);
                        var 绵羊自繁 = Convert.ToDouble(row.IsNull(18) ? 0 : row[18]);
                        var 购进 = Convert.ToDouble(row.IsNull(19) ? 0 : row[19]);
                        var 山羊购进 = Convert.ToDouble(row.IsNull(20) ? 0 : row[20]);
                        var 绵羊购进 = Convert.ToDouble(row.IsNull(21) ? 0 : row[21]);
                        var 期内减少 = Convert.ToDouble(row.IsNull(22) ? 0 : row[22]);
                        var 自宰 = Convert.ToDouble(row.IsNull(23) ? 0 : row[23]);
                        var 出售 = Convert.ToDouble(row.IsNull(24) ? 0 : row[24]);
                        var 出售羊金额 = Convert.ToDouble(row.IsNull(25) ? 0 : row[25]);
                        var 出售羊重量 = Convert.ToDouble(row.IsNull(26) ? 0 : row[26]);
                        var 当季绵羊毛产量 = Convert.ToDouble(row.IsNull(27) ? 0 : row[27]);
                        var 当季山羊绒产量 = Convert.ToDouble(row.IsNull(28) ? 0 : row[28]);
                        MassSheepAndGoatMsgs MassSheepAndGoat = new MassSheepAndGoatMsgs(
                            市名, 县名, 乡名, 户主姓名, 养殖户类型, 当季出栏数, 期末存栏合计, 山羊存栏, 能繁殖母山羊存栏, 绵羊存栏, 能繁殖母绵羊存栏, 期内增加
                            , 自繁, 山羊自繁, 绵羊自繁, 购进, 山羊购进, 绵羊购进, 期内减少, 自宰, 出售, 出售羊金额, 出售羊重量, 当季绵羊毛产量, 当季山羊绒产量
                        );
                        MassSheepAndGoatCln.Add(MassSheepAndGoat);
                    }

                }
            }
            catch (Exception ex)
            {
                StaticLogger.SystemError($"出现错误,错误信息{ex.Message},{Environment.NewLine}堆栈为{ex.StackTrace},{Environment.NewLine}对象为{ex.Source},{Environment.NewLine}方法为{ex.TargetSite}");
                await MyMessagebox.ShowAsyncInMainWindow($"出现错误,错误内容为{ex.Message}", Icon.Error);
            }
        }

        /// <summary>
        /// 展示本季度所有养殖户的情况
        /// </summary>
        /// <returns></returns>
        private async Task ShowMsgByAllFarmerInDataGridThisSeasonAsync()
        {
            try
            {
                string[] allNeedTables = { withYSTableNameNow };
                MyProgressBar _progressBar = new MyProgressBar();
                _progressBar.Show();
                var progress = new Progress<int>(value => _progressBar.UpdateProgress(value));
                string modifyOutcome = await Task.Run(() => Modify.ModifyTables(allNeedTables, progress));
                _progressBar.Close();
                if (modifyOutcome != null)
                {
                    await MyMessagebox.ShowAsyncInMainWindow($"市县乡名称修正时出错,错误内容为:{modifyOutcome},一般不影响数据展示,建议排查", Icon.Warning);
                }
                MassSheepAndGoatCln.Clear();
                DTAll = 羊规模户信息.GetDTAllIndicatorsOfAllOwnersLocalThisSeason(SelectedCity, SelectedCountry, SelectedTown);

                if (DTAll!=null)
                {

                    for (global::System.Int32 i = 0; i < DTAll.Rows.Count; i++)
                    {
                        DataRow row = DTAll.Rows[i];
                        var 市名 = row[0].ToString();
                        var 县名 = row[1].ToString();
                        var 乡名 = row[2].ToString();
                        var 户主姓名 = row[3].ToString(); ;
                        var 养殖户类型 = row[4].ToString(); ;
                        var 当季出栏数 = Convert.ToDouble(row.IsNull(5) ? 0 : row[5]);
                        var 期末存栏合计 = Convert.ToDouble(row.IsNull(6) ? 0 : row[6]);
                        var 山羊存栏 = Convert.ToDouble(row.IsNull(7) ? 0 : row[7]);
                        var 能繁殖母山羊存栏 = Convert.ToDouble(row.IsNull(8) ? 0 : row[8]);
                        var 绵羊存栏 = Convert.ToDouble(row.IsNull(9) ? 0 : row[9]);
                        var 能繁殖母绵羊存栏 = Convert.ToDouble(row.IsNull(10) ? 0 : row[10]);
                        var 期内增加 = Convert.ToDouble(row.IsNull(11) ? 0 : row[11]);
                        var 自繁 = Convert.ToDouble(row.IsNull(12) ? 0 : row[12]);
                        var 山羊自繁 = Convert.ToDouble(row.IsNull(13) ? 0 : row[13]);
                        var 绵羊自繁 = Convert.ToDouble(row.IsNull(14) ? 0 : row[14]);
                        var 购进 = Convert.ToDouble(row.IsNull(15) ? 0 : row[15]);
                        var 山羊购进 = Convert.ToDouble(row.IsNull(16) ? 0 : row[16]);
                        var 绵羊购进 = Convert.ToDouble(row.IsNull(17) ? 0 : row[17]);
                        var 期内减少 = Convert.ToDouble(row.IsNull(18) ? 0 : row[18]);
                        var 自宰 = Convert.ToDouble(row.IsNull(19) ? 0 : row[19]);
                        var 出售 = Convert.ToDouble(row.IsNull(20) ? 0 : row[20]);
                        var 出售羊金额 = Convert.ToDouble(row.IsNull(21) ? 0 : row[21]);
                        var 出售羊重量 = Convert.ToDouble(row.IsNull(22) ? 0 : row[22]);
                        var 当季绵羊毛产量 = Convert.ToDouble(row.IsNull(23) ? 0 : row[23]);
                        var 当季山羊绒产量 = Convert.ToDouble(row.IsNull(24) ? 0 : row[24]);
                        MassSheepAndGoatMsgs MassSheepAndGoat = new MassSheepAndGoatMsgs(
                            市名, 县名, 乡名, 户主姓名, 养殖户类型, 当季出栏数, 期末存栏合计, 山羊存栏, 能繁殖母山羊存栏, 绵羊存栏, 能繁殖母绵羊存栏, 期内增加
                            , 自繁, 山羊自繁, 绵羊自繁, 购进, 山羊购进, 绵羊购进, 期内减少, 自宰, 出售, 出售羊金额, 出售羊重量, 当季绵羊毛产量, 当季山羊绒产量
                        );
                        MassSheepAndGoatCln.Add(MassSheepAndGoat);
                    }
                }
            }
            catch (Exception ex)
            {
                StaticLogger.SystemError($"出现错误,错误信息{ex.Message},{Environment.NewLine}堆栈为{ex.StackTrace},{Environment.NewLine}对象为{ex.Source},{Environment.NewLine}方法为{ex.TargetSite}");
               // await MyMessagebox.ShowAsyncInMainWindow($"出现错误,错误信息为{ex.Message}", Icon.Error);
                throw;
            }
        }

        private async Task ExpertToExcelAll()
        {
            string path = Path.Combine(MyDataFolderOperator.AuditOutcomeFolderPathInAuditProgramFolder, $"A211表{Global.StrYearNow}{Global.StrSeasonNow}{SelectedPeriod}所有指标汇总分析结果{DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒")}.xlsx");
            if (MassSheepAndGoatCln != null)
            {
                bool isExpertSuccess = ExpertToExcelHelper.ExportToExcel<MassSheepAndGoatMsgs>(MassSheepAndGoatCln, path);
                if (isExpertSuccess)
                {
                    await MyMessagebox.ShowAsyncInMainWindow("导出成功,保存在“审核结果“文件夹中”", Icon.Success);
                }
                else
                {
                    await MyMessagebox.ShowAsyncInMainWindow("导出出错", Icon.Error);
                }
            }
            else
            {
                await MyMessagebox.ShowAsyncInMainWindow("未进行分析!", Icon.Error);
            }
        }
        private async Task ExpertToExcelSingle()
        {
            string path = Path.Combine(MyDataFolderOperator.AuditOutcomeFolderPathInAuditProgramFolder, $"A4211表{Global.StrYearNow}{Global.StrSeasonNow}{SelectedIndicator.Value}单个指标汇总分析结果{DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒")}.xlsx");
            if (SingleIndicatorSheepAndGoatCln != null)
            {
                bool isExpertSuccess = ExpertToExcelHelper.ExportToExcel<SingleIndicatorMsg>(SingleIndicatorSheepAndGoatCln, path);
                if (isExpertSuccess)
                {
                    await MyMessagebox.ShowAsyncInMainWindow("导出成功,保存在“审核结果“文件夹中”", Icon.Success);
                }
                else
                {
                    await MyMessagebox.ShowAsyncInMainWindow("导出出错", Icon.Error);
                }
            }
            else
            {
                await MyMessagebox.ShowAsyncInMainWindow("未进行分析!", Icon.Error);
            }
        }



    }
}
